package com.cskaoyan.mall.commons.util;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.bean.Member;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author wjj
 * @date 2023-01-10 12:28
 * @describe
 */

public class MemberUtils {
    public static Member getLoginMember(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String token = null;
        Member member = null;
        for (Cookie cookie : cookies) {
            if ("access_token".equals(cookie.getName())) {
                token = cookie.getValue();
            }
        }
        if (token != null) {
            JwtTokenUtils build = JwtTokenUtils.builder().token(token).build();
            String memberStr = build.freeJwt();
            member = JSON.parseObject(memberStr, Member.class);
        }
        return member;
    }
}